﻿using Aliphant.Mvvm.XEvent.Events;
using Microsoft.Win32;
using System;

namespace CommonLibrary.Helper
{
    public class WindowHelper
    {
        public static void SaveFileWindow(WindowEvent windowEvent)
        {
            var fileDialog = new SaveFileDialog();
            if(windowEvent.Data is string filter && !string.IsNullOrWhiteSpace(filter))
            {
                fileDialog.Filter = filter;
            }
            var result = fileDialog.ShowDialog();
            if(result.HasValue && result.Value )
            {
                if(windowEvent.CallBack is Action<string> action)
                {
                    action(fileDialog.FileName);
                }
            }
        }

        public static void SelectFileWindow(WindowEvent windowEvent)
        {
            var fileDialog = new OpenFileDialog();
            if(windowEvent.Data is string filter && !string.IsNullOrWhiteSpace(filter))
            {
                fileDialog.Filter = filter;
            }
            var result = fileDialog.ShowDialog();
            if(result.HasValue && result.Value)
            {
                if(windowEvent.CallBack is Action<string> action)
                {
                    action(fileDialog.FileName);
                }
            }
        }

        public static void SelectFilesWindow(WindowEvent windowEvent)
        {
            var fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            if (windowEvent.Data is string filter && !string.IsNullOrWhiteSpace(filter))
            {
                fileDialog.Filter = filter;
            }
            var result = fileDialog.ShowDialog();
            if (result.HasValue && result.Value)
            {
                if (windowEvent.CallBack is Action<string> action)
                {
                    string fn = string.Join(";", fileDialog.FileNames);
                    action(fn);
                }
            }
        }
    }
}
